package com.letoken.platform.repo.app.manager;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.letoken.platform.repo.admin.res.config.QueryRechargeTokenListRes;
import com.letoken.platform.repo.admin.res.config.QueryTokenListRes;
import com.letoken.platform.repo.app.dao.TokenMapper;
import com.letoken.platform.repo.app.po.Token;
import com.letoken.platform.repo.app.redis.AppWalletTokenRedis;
import com.letoken.platform.repo.app.res.assets.QueryTokenAssetsRes;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;
import org.web3j.abi.datatypes.Int;

import java.util.List;

/**
 * @author letokenlabs
 * @date 2022/11/18$
 */
@Log4j2
@Repository
public class TokenManager {

    @Autowired
    private TokenMapper tokenMapper;


    public String queryTokenAddressByName(Integer chainId, String usdt) {
        return null;
    }

    /**
     * 获取代币对象
     * @param token (代币名称或合约地址)
     * @return
     */
    public Integer getTokenIdWithRedis(String token){
        String tokenId = null;
        if(token.startsWith("0x")) {
            tokenId = AppWalletTokenRedis.me().getStrValue(token);
            if (StringUtils.isEmpty(tokenId)) {
                 Token tokenInfo = queryByAddress(token);
                if (tokenInfo != null) {
                    AppWalletTokenRedis.me().put(token, tokenInfo.getId());
                    tokenId = tokenInfo.getId().toString();
                }
            }
        }
        else {
            tokenId = AppWalletTokenRedis.me().getStrValue(token);
            if (StringUtils.isEmpty(tokenId)) {
                Token tokenInfo = queryTokenByCurrency(token);
                if (tokenInfo != null) {
                    AppWalletTokenRedis.me().put(token, tokenInfo.getId());
                    tokenId = tokenInfo.getId().toString();
                }
            }
        }
        return StringUtils.isEmpty(tokenId)? 0: Integer.parseInt(tokenId);
    }

    /**
     * 根绝token地址查询代币详情
     *
     * @param address
     * @return
     */
    public Token queryByAddress(String address) {
        QueryWrapper<Token> query = new QueryWrapper<>();
        query.eq("token_address", address);
        return tokenMapper.selectOne(query);
    }

    public Token queryById(Integer tokenId) {
        return tokenMapper.selectById(tokenId);
    }


    /**
     * 根据代币的名称查询代币详情
     *
     * @param usdt
     * @return
     */
    public Token queryTokenByCurrency(String usdt) {
        QueryWrapper<Token> query = new QueryWrapper<>();
        query.eq("token_currency", usdt).eq("is_use", 1);
        return tokenMapper.selectOne(query);
    }

    /**
     * 查询所有使用的代币
     *
     * @return
     */
    public List<Token> queryList() {
        QueryWrapper<Token> query = new QueryWrapper<>();
        query.eq("is_use", 1);
        return tokenMapper.selectList(query);
    }

    public List<QueryRechargeTokenListRes> queryTokenList() {
        return tokenMapper.queryTokenList();
    }

    public List<QueryTokenListRes> queryReTokenList() {
        return tokenMapper.queryReTokenList();

    }

    public List<QueryTokenAssetsRes> queryTokenAssetsByUser(Integer userId){
        return tokenMapper.queryTokenAssetsByUser(userId);
    }

    public List<QueryTokenAssetsRes> queryTokenAssetsByUserAndNetType(Integer userId, String netType){
        return tokenMapper.queryTokenAssetsByUserAndNetType(userId, netType);
    }
}
